<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Data_fetcher extends CI_Model {
	
	
	function fetch_table($table,$order=null,$limit=null,$where=null)
	{				
		
		if($order!=null)
			$this->db->order_by($order[0],$order[1]);
		elseif($table == 'tb_mas_content')
			$this->db->order_by('dteStart','desc');
		
		if($limit!=null)
			$this->db->limit($limit);
			
		if($where!=null)
			$this->db->where($where);
		
		$data =  $this->db
						->get($table)
						->result_array();
						
		return $data;
						
	}
	
	function fetch_single_data($table,$id=1)
	{
		$data = $this->db
					 ->get_where($table,array('intId'=>$id))
					 ->result_array();
		return current($data);
	}
	
	
	function count_results($table)
	{
		return $this->db->count_all($table);
	}
	
	function get_validation_items($table)
	{
		$data = $this->db
					 ->get_where('su-tb_sys_validation',array('strTable'=>$table))
					 ->result_array();
		return $data;
	}
	
	function get_achievements_by_candidate($id)
	{
		$data = $this->db
					 ->order_by('intID','desc')
					 ->get_where('tb_mas_candidates_achievements',array('fk-tb_mas_candidates'=>$id))
					 ->result_array();
		return $data;
	}
	
	function search_by_candidate_name($words)
	{
		
		/*foreach($words as $value) //via first name
		{
			$this->db->or_like('strFname',$value);
		}
		
		foreach($words as $value) //via middle name
		{
			$this->db->or_like('strMname',$value);
		}
		
		foreach($words as $value) //via last name
		{
			$this->db->or_like('strLname',$value);
		}
		
		foreach($words as $value) //via nick name
		{
			$this->db->or_like('strNname',$value);
		}*/
		
		$this->db->having("strFname = '".$words[0]."'");
		
		$this->db->having("strLname = '".$words[sizeOf($words)-1]."'");
		
		$data = $this->db
					 ->get('tb_mas_candidates')
					 ->result_array();
		return current($data);
		//return $this->db->last_query();
	}
	
	function get_candidate_record_by_type($id,$type,$amount=1)
	{
		
		$data = $this->db
						->limit($amount)
						->get_where('tb_mas_candidates_records',Array('fk-tb_mas_candidates'=>$id,'enumCategory'=>$type,'enumStatus'=>'enabled'))						
						->result_array();
		return $data;
	}

}